home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / infosrvr / dev / libhtml_.tar / main.c < prev    next >
C/C++ Source or Header  |  1993-01-21  |  4KB  |  172 lines

  1. /* main.c -- test driver for WWW io routines
  2.  * $Id: SGMLmain.c,v 1.3 93/01/06 18:40:27 connolly Exp Locker: connolly $
  3.  */
  4.  
  5. #include "HTMLwriter.h"
  6. #include "MIFwriter.h"
  7. #include "plaintext.h"
  8. #include "SGML.h"
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. static HMWriteProc filewrite;
  14.  
  15. static int
  16.   filewrite(out, buf, n)
  17. HMStream out;
  18. CONST VOIDPTR buf;
  19. unsigned n;
  20. {
  21.   return (int)fwrite(buf, 1, n, (FILE*)out);
  22. }
  23.  
  24. /* Traditional C implements getc only as a macro... */
  25.  
  26. static int
  27.   getc_function(f)
  28. FILE* f;
  29. {
  30.   return getc(f);
  31. }
  32.  
  33.  
  34. main(argc, argv)
  35.      int argc;
  36.      char* argv[];
  37. {
  38.   if(argc <= 1)
  39.     sgml_test();
  40.   else{
  41.     HMDoc* writer;
  42.     HMDoc_Class* wclass;
  43.     HMParseProc* read;
  44.  
  45.     if(strcmp(argv[1], "plaintext"))
  46.       read = SGML_parseInstance;
  47.     else
  48.       read = Plaintext_parse;
  49.     
  50.     if(argc < 3 || strcmp(argv[2], "MIF")){
  51.       wclass = &HTMLwriter;
  52.       writer = wclass->writer((HMStream)stdout, filewrite);
  53.     }else{
  54.       wclass = &MIFwriter;
  55.       writer = wclass->fileWriter(stdout);
  56.     }
  57.  
  58.     if(argc < 4){
  59.       (read)((HMStream)stdin, (HMGetcProc*)getc_function, writer, wclass);
  60.     }else{
  61.       /* test incore */
  62.       HMDoc* incore = InCore_new();
  63.  
  64.       (read)((HMStream)stdin, (HMGetcProc*)getc_function, incore, &InCore);
  65.  
  66.       InCore_traverse(incore, writer, wclass);
  67.       InCore.delete(incore);
  68.     }
  69.     (wclass->delete)(writer);
  70.   }
  71.  
  72.   exit(0);
  73. }
  74.  
  75. sgml_test()
  76. {
  77.   int read;
  78.   char buffer[72 + 1];
  79.   int content = SGML_MIXED;
  80.   int lookahead = EOF;
  81.   char name[SGML_NAMELEN+1];
  82.   char name_chars;
  83.   char value[SGML_LITLEN+1];
  84.   char out[sizeof(value) * 6];
  85.  
  86.   while((read = SGML_read((HMStream)stdin, (HMGetcProc*)getc_function,
  87.                buffer, sizeof(buffer) - 1,
  88.                content,
  89.                &lookahead)) != EOF){
  90.     switch(read){
  91.     case SGML_end_tag:
  92.     case SGML_start_tag:
  93.       name_chars = SGML_read_name((HMStream)stdin, (HMGetcProc*)getc_function,
  94.                   name, &lookahead);
  95.       name[name_chars] = '\0';
  96.       if(read == SGML_end_tag){
  97.     printf("</%s>", name);
  98.     content = SGML_MIXED;
  99.       }else{
  100.     printf("<%s", name);
  101.  
  102.     /*
  103.      * certain tags change parsing mode
  104.      * @@ this should be table-driven
  105.      */
  106.     if(!strcmp(name, "TITLE") ||
  107.        !strcmp(name, "XMP") ||
  108.        !strcmp(name, "LISTING")){
  109.       content = SGML_RCDATA;
  110.     }
  111.  
  112.     while(isalpha(lookahead)){ /* iterate over attributes */
  113.       name_chars = SGML_read_name((HMStream)stdin, (HMGetcProc*)getc_function,
  114.                       name, &lookahead);
  115.       name[name_chars] = '\0';
  116.       if(lookahead == '='){
  117.         int len;
  118.         lookahead = EOF;
  119.         read = SGML_read_value((HMStream)stdin,
  120.                    (HMGetcProc*)getc_function,
  121.                    value,
  122.                    &lookahead);
  123.         value[read] = '\0';
  124.         len = SGML_replen(value, "\"&");
  125.  
  126.         SGML_repcpy(out, value, "\"&");
  127.         printf(" %s=\"", name);
  128.         fwrite(out, sizeof(char), len, stdout);
  129.         fputs("\"", stdout);
  130.       }
  131.     }
  132.     printf(">");
  133.       }
  134.  
  135.       /* look for tag close */
  136.       while(isspace(lookahead))
  137.       lookahead = getc(stdin);
  138.       lookahead = EOF;
  139.       break;
  140.  
  141.     case SGML_entity:
  142.       /* support old <, >, & representations */
  143.       if(!strcmp(buffer, "lt"))
  144.     printf("<");
  145.       else if(!strcmp(buffer, "gt"))
  146.     printf(">");
  147.       else if(!strcmp(buffer, "amp"))
  148.     printf("&");
  149.       else
  150.     printf("&%s;", buffer);
  151.       break;
  152.  
  153.     case SGML_record_end:
  154.       printf("\n");
  155.       break;
  156.  
  157.     default:
  158.       if(read>0){
  159.     int len;
  160.     
  161.     buffer[read] = 0;
  162.     len = SGML_replen(buffer, "<>&");
  163.     
  164.     SGML_repcpy(out, buffer, "<>&");
  165.     fwrite(out, sizeof(char), len, stdout);
  166.       }
  167.     }
  168.   }
  169.   exit(0);
  170. }
  171.  
  172.